package com.jstyle.jclifexd.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.JstyleDevice;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ScanDeviceAdapter extends BaseAdapter {
    private List<BluetoothDevice> devices=new ArrayList<>();
    private HashMap<BluetoothDevice, Integer> rssis;
    private HashMap<BluetoothDevice, String> names;
    private LayoutInflater mInflator;
 //  private int  deviceDrawable;
    public ScanDeviceAdapter(Context context, List<BluetoothDevice> device) {
        this.devices = device;
        mInflator = LayoutInflater.from(context);
        this.rssis = new HashMap<BluetoothDevice, Integer>();
        this.names = new HashMap<BluetoothDevice, String>();
    }

 //   public void setDeviceDrawable(int deviceDrawable ){
     //   this.deviceDrawable=deviceDrawable;
   // }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return devices.size();
    }

    @Override
    public BluetoothDevice getItem(int position) {
        // TODO Auto-generated method stub
        return devices.get(position);
    }

    public String getName(BluetoothDevice device) {
        return names.get(device);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_rssi = (ImageView) view.findViewById(R.id.iv_rssi);
            viewHolder.iv_product = (ImageView) view.findViewById(R.id.iv_item_product);
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
            viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        BluetoothDevice device = devices.get(position);
        if(device!=null){
            final String deviceName = names.get(device);
            viewHolder.iv_product.setImageResource(getDeviceType(deviceName));
            if (deviceName != null && deviceName.length() > 0){
                viewHolder.deviceName.setText(deviceName);
            }else{
                viewHolder.deviceName.setText("Unkown Device");
            }




            //设备mac地址为空，部分三星手机出现
            viewHolder.deviceAddress.setText(TextUtils.isEmpty(device.getAddress())?"":device.getAddress());
            viewHolder.deviceRssi.setText("Rssi:" + rssis.get(device));
            final int rssiPercent = Math.abs(rssis.get(device));
            viewHolder.iv_rssi.setImageLevel(rssiPercent);
        }

        return view;
    }
    private int  getDeviceType(String name){
        /*int  id=R.drawable.product_1755;
        if(name.contains(JstyleDevice.device_1755)){
            id=R.drawable.product_1755;
        }else if(name.contains(JstyleDevice.device_1790)){
           id=R.drawable.product_1790;
        }else if(name.contains(JstyleDevice.device_1810)){
            id=R.drawable.product_1810;
        }else if(name.contains(JstyleDevice.device_1810_03)){
            id=R.drawable.product_1810;
        }else if(name.contains(JstyleDevice.device_1860)){
            id=R.drawable.product_1860;
        }else if(name.contains(JstyleDevice.device_1936)||name.toLowerCase().contains(JstyleDevice.device_body)){
            id=R.drawable.product_1936;
        }*/
        int  id=R.drawable.product_1810;
        if(name.toLowerCase().contains(JstyleDevice.device_1810)||name.toLowerCase().contains(JstyleDevice.Everlast_HR)){
            id=R.drawable.product_1810;
        }
     return  id;
    }
    public void clear() {
        devices.clear();
        notifyDataSetChanged();
    }

    public void addDevice(BluetoothDevice device, int rssi, String name) {
        // TODO Auto-generated method stub
        if (!devices.contains(device)) {
            devices.add(device);
            rssis.put(device, rssi);
            names.put(device, name);
            notifyDataSetChanged();
        }
    }

    static class ViewHolder {
        TextView deviceName;
        TextView deviceAddress;
        TextView deviceRssi;
        ImageView iv_rssi;
        ImageView iv_product;
    }
}
